Skip to content

Write signed zero as 0.0 in every written float (#105) - #110

Merged
guijacquemet merged 5 commits into
mainfrom
claude/signed-zero-105
Nov 17, 2026
Merged

guijacquemet merged 5 commits into
mainfrom
claude/signed-zero-105

Conversation

@guijacquemet

Copy link
Copy Markdown
Contributor

Closes #105. Owner decision of 17 November 2026: canonicalise signed zero to 0.0 (rule 2 of the issue). Recorded as plan N71. Targeted for v0.7.1.

What changes

  • New pure module float_text with canonical_zero(x) = float(x) + 0.0 and float_text(x) = repr(canonical_zero(x)). This is the one place a float becomes written text. x + 0.0 changes no float except -0.0, which becomes 0.0 under round-to-nearest. NaN and ±inf are written as before.
  • These paths now go through it:
    • csv_io._text, used for Intensity_Min/MaxIntensity_<ID> and Histogram_BinningMin/Max_<ID>.
    • csv_io._tail_text, used for the underflow and overflow mean and std.
    • Parameters.to_columns(), used for Normalization_Offset/Scale_<ID>.
    • ImageSummary.to_row(), for its float values. The package writes no file here, but these rows are the §7.1 columns a calling brick writes.
    • normalization._exact_decimal (percentile_name). This path already did + 0.0 and now uses the helper.
  • The package has no JSON writer, so there was no JSON path to change. The JSON files under tests/data are test goldens written by test scripts.
  • In-memory values (PlaneHistogram, Parameters, ImageSummary fields) keep their sign. No algorithm, API, schema version or message changes. The contract (§6.1, §7.1, §8.2), plan N71 and CHANGELOG (Unreleased) are updated.

Goldens and bytes

  • None of the recorded goldens or parity tables (tests/data/*.json, golden_pixels.npz, spec fixtures) contains -0.0. An exact-regex search found none.
  • tools/dump_outputs.py gives the same digests before and after: default 032b395b…, --corpus 0111f8bd…. Neither dump contains -0.0.
  • Only files that held -0.0 change, and only in those cells. The new value reads back as an equal float.

Tests

tests/test_signed_zero.py (33 tests) covers:

  • a computed all--0.0 float32 plane, written as 0.0 in every float column;
  • planes that mix signed zeros: all 24 permutations of [-0.0, 0.0, -0.0, 0.0] plus three mixed planes, each written in three element orders, which must give identical bytes;
  • tail mean and std;
  • parameter columns from a fit whose offset is -0.0 (the round trip still works);
  • summary rows;
  • percentile_name;
  • unit tests of the helper (subnormals, ±inf, NaN, NumPy scalars).

On v0.7.0 / origin/main (helper-only tests removed so the file imports), the suite gives 29 failed, 3 passed. Every writer path fails there. With this change all 33 pass.

Full suite, run in two halves (Python 3.13, [dev,histology]):

  • first half: 2094 passed, 2 skipped (csbdeep is not installed);
  • second half: 679 passed.

tools/check.py --rules and ruff format --check are clean.

Release note

Draft release PR #109 merges cleanly with this branch in git terms. Its v0.7.1 summary says "every output bitwise that of v0.7.0" and "No … file-format change", so it needs a small edit to mention #105 after this merges.


Generated by Claude Code

Owner decision 17 November 2026 (plan N71): every float the package
writes goes through one helper, float_text.canonical_zero, so a plane
whose pixel_min or pixel_max is -0.0 on one platform and +0.0 on
another writes the same bytes. Covers the float cells of
plane_histograms.csv, Parameters.to_columns(), ImageSummary.to_row()
and percentile_name. Computed values keep their sign; no algorithm,
schema version or message changes. tools/dump_outputs.py digests are
unchanged and no recorded golden holds -0.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc

@guijacquemet guijacquemet left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review: #110 (head 19590b3, fixes #105)

Verdict: CHANGES NEEDED. The fix is small and the core change is correct. The main changes needed: one to_row byte change on non-zero values that isn't documented, and a claim scope that's wider than what the code covers.

Verified

  • Fresh clone, venv, pip install --no-cache-dir -e .[dev,histology], NumPy 2.5.5, Python 3.13. At head the suite ran in two halves: 2094 passed / 2 skipped, then 679 passed. The 2 skips are tests/test_rgb.py:82, which needs csbdeep (not installed). python tools/check.py passed: code rules, ruff, 2773 passed / 2 skipped.
  • Ran tests/test_signed_zero.py against origin/main (30992e9), with the two helpers inlined as a shim: 29 failed, 4 passed, as claimed.
  • tools/dump_outputs.py gives the same digests on main and on head: 032b395b…1102 (1788 lines) and --corpus 0111f8bd…009e (4764 lines). These match the CHANGELOG.
  • canonical_zero: under round-to-nearest, x + 0.0 == x bitwise for every float except -0.0. NaN still prints nan, ±inf and subnormals are unchanged.
  • No byte change for any value the library computes. HistogramTail (integrity.py:110) and Parameters (parameters.py:99-100) already convert to Python float. _wide_histogram produces Python numbers, and _key_value uses .item(). PooledHistogram.percentile/median return float(...) (pooling.py:179), and so does _saturated_fraction. So repr(canonical_zero(x)) == repr(x) on every non-zero path the library can reach.
  • No JSON writer: there is no json in src/. The only file the package writes is csv_io.py:257.
  • The only -0.0 grep hit in the goldens (tests/data/stain_v060_parity.json) is -0.06…, a false positive. The "no golden holds -0.0" claim holds.
  • Plan numbering (item 65, N71 after N70) and the contract sections (§6.1, §7.1, §8.2) are correct. Not bumping schema_version is justified: 0.0 == -0.0 and readers parse both.

Findings

  1. ImageSummary.to_row() changes text and type for non-zero values when the caller supplies the values (summaries.py:158-167). Must fix or document. canonical_zero runs float() on every stored value. On a directly built ImageSummary, this changes what a caller writes:

    • np.float32(0.1) becomes 0.10000000149011612. csv.writer used to write 0.1.
    • 5 becomes 5.0.
    • np.float64 becomes float.

    Rows from image_summaries() are unchanged, because they already hold Python floats. But the CHANGELOG says "no API … change", and this is a byte change on non-zero values for public-constructor input. Simplest fix: canonicalise only zeros, e.g. 0.0 if value == 0 else value (or value + 0.0 only when value == 0). That keeps the type and text of every other value. Alternatively, document the float() conversion as intended.

  2. csv_io._text (csv_io.py:585-590) only catches float subclasses. A PlaneHistogram built directly with np.float32(-0.0) limits still writes -0.0, both before and after this PR (reproduced). compute_* never produces this, because it yields Python floats. Separately, np.float64 limits used to write np.float64(-0.0) / np.float64(0.1), which can't be read back. They now write 0.0 / 0.1. That's a side fix the PR doesn't mention. Suggest one of:

    • make PlaneHistogram (or check_histogram) convert limits to Python int/float at construction, as HistogramTail and Parameters already do;
    • extend _text to np.floating with zero-only handling.

    Either way, add a regression test.

  3. The claim "every float the package writes" is wider than the code. These to_dict() methods are documented as JSON-compatible dicts for the run configuration, the same role as to_row, and they still pass a user-given -0.0 through:

    • NormalizationSettings.to_dict() (settings.py:172-188, percentile)
    • HistogramSettings.to_dict() (ranges.py:111-118, robust_quantiles)
    • IntensityCheckSettings.to_dict() (checks.py:135-142, saturation_limit)

    Verified: json.dumps(NormalizationSettings(percentile=(-0.0, 99.8)).to_dict()) gives [-0.0, 99.8], while method_name for the same settings gives percentile 0-99.8. This echoes user input, so it is deterministic and is not the platform problem in #105. Either send these floats through canonical_zero too, or narrow the wording in the CHANGELOG, plan N71 and the float_text module docstring so they list exactly what is covered.

  4. Note, no change needed: message text can still show -0 (checks._text .6g, ranges._percent :g, the !r values in integrity.py). The PR states that messages are out of scope, which is consistent.

  5. Nit: the line edited at docs/contract.md:991 is now about 120 characters. The rest of the file wraps at about 100.

Not merged or pushed.


Generated by Claude Code

to_row() ran float() on every stored value, so a caller-built
ImageSummary holding np.float32(0.1) or 5 wrote 0.10000000149011612 or
5.0. The new float_text.positive_zero replaces a float zero of either
sign (Python or NumPy) with 0.0 and returns every other value as given,
so non-zero values keep their v0.7.0 type and text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
…view 2)

csv_io._text only canonicalised float subclasses, so a caller-built
PlaneHistogram with np.float32(-0.0) limits still wrote -0.0. _text now
passes every value through positive_zero first. Side fix, documented in
the CHANGELOG: np.float64 limits used to be written as np.float64(...),
which the reader refuses; they are now written as the Python float's
repr. np.float32 and integer limits keep their str text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
NormalizationSettings.to_dict() (percentile), HistogramSettings.to_dict()
(robust_quantiles) and IntensityCheckSettings.to_dict()
(saturation_limit) are the JSON run configuration a caller writes; a
user-given -0.0 now becomes 0.0 through positive_zero, zeros only. The
CHANGELOG, plan N71 and the float_text docstring list these paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc

Copy link
Copy Markdown
Contributor Author

Review findings addressed, one commit each (head 045d25e):

  1. 4de489a: ImageSummary.to_row() now changes only float zeros, via the new float_text.positive_zero. Every other value keeps its type and text (np.float32(0.1), 5, np.float64), and a regression test checks that the DictWriter bytes are unchanged.
  2. 49f3737: csv_io._text now writes NumPy float zeros (np.float32 and np.float64 -0.0) as 0.0. Side fix, documented in the CHANGELOG: np.float64 limits used to be written as np.float64(...), which the reader could not read. np.float32 and integer limits keep their str text.
  3. 0b34786: the three settings to_dict() methods (percentile, robust_quantiles, saturation_limit) now go through positive_zero. The CHANGELOG, N71 and the float_text docstring list these paths.
  4. 045d25e: re-wrapped the edited lines to about 100 characters in docs/contract.md, docs/plan.md and CHANGELOG.

tools/dump_outputs.py digests are unchanged: 032b395b… and 0111f8bd…. Tests, run in two halves: 2094 passed / 2 skipped (csbdeep not installed), then 692 passed. CI is green.

🤖 Generated with Claude Code


Generated by Claude Code

@guijacquemet guijacquemet left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review: #110 at head 045d25e (4 commits on top of 19590b3)

Verdict: MERGE. Each of the four earlier findings is fixed, and I found no byte change that the PR doesn't document.

What I checked

Fresh clone, venv, pip install --no-cache-dir -e .[dev,histology], NumPy 2.5.5, Python 3.13.

  • Test suite at head, run in two halves: 2094 passed / 2 skipped, then 692 passed. The 2 skips are test_rgb.py:82, which needs csbdeep (not installed). python tools/check.py passes: code rules, ruff, 2786 passed / 2 skipped.

  • New tests fail on the pre-fix code. I ran the head's tests/test_signed_zero.py against 19590b3, with positive_zero inlined as a shim. Result: 5 failed, 41 passed. Each code finding has a failing test:

    • to_row keeping its values: test_summary_rows_keep_every_other_value_as_given
    • np.float32(-0.0) limits: test_numpy_negative_zero_limits_write_0_0[float32]
    • the three settings dictionaries: test_settings_dictionaries_write_0_0 ×3

    The np.float64 cases already passed on 19590b3, which had fixed them.

  • tools/dump_outputs.py digests are the same on main and on head: 032b395b…1102 (1788 lines) and --corpus 0111f8bd…009e (4764 lines).

  • Written output compared, main against head, for every value type, with the same probe script on both commits:

    • Limits in plane_histograms.csv (Python float, np.float64, np.float32, np.float16, int, np.int64/int32/uint16):
      • Non-zero values give identical text. The one exception is np.float64, which main wrote as np.float64(0.25). The reader refuses that text, so no valid file contained it. Head writes 0.25, and the CHANGELOG documents this as a side fix.
      • Integers and NumPy integers give identical text, including 0.
      • Every float zero of either sign gives 0.0, for all four float types.
    • ImageSummary.to_row():
      • np.float32(0.1), np.float64(0.1), 5, np.int64(0), None and NaN come back as the same objects as on main, and csv writes the same text for them.
      • -0.0 in Python, np.float64, np.float32 or np.float16 becomes the Python float 0.0.
    • to_dict() of NormalizationSettings (percentile), HistogramSettings (robust_quantiles) and IntensityCheckSettings (saturation_limit): JSON for -0.0 in Python, np.float64 or np.float32 input is now 0.0. A non-zero value such as 0.25 is unchanged. __post_init__ already converts these values to Python floats, so no type change is possible.

Handling NumPy limits in _text rather than at construction

This is acceptable. Converting limits at construction would also convert the NumPy integer limit types that #104 keeps. positive_zero in _text (csv_io.py:585-594) changes only float zeros. isinstance(value, float) then sends np.float64 to repr(float) and leaves np.float32 on str, as before. The np.float32 str text does not round-trip to the exact float32 value, but that was already true on main and is outside this PR.

Docs

  • The CHANGELOG, plan N71 and the float_text module docstring now separate the two kinds of output:
    • text the package writes itself goes through canonical_zero;
    • values handed to a caller to write go through positive_zero.
  • The CHANGELOG states the np.float64 side fix.
  • Not bumping schema_version is still justified.

Nits (non-blocking)

  • docs/contract.md §7.1: after the rewrap, the line holding only `summary_columns(channel_ids)` the names is short. Cosmetic.
  • §7.1 still says "numbers as floats". to_row now passes a value the caller supplied through with its own type, e.g. an int. That's fine for summaries made by image_summaries(); the wording could mention it later.

Not merged or pushed.


Generated by Claude Code

@guijacquemet
guijacquemet merged commit dcdcfd0 into main Nov 17, 2026
10 checks passed
@guijacquemet
guijacquemet deleted the claude/signed-zero-105 branch November 17, 2026 05:27
guijacquemet pushed a commit that referenced this pull request Nov 17, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
guijacquemet pushed a commit that referenced this pull request Nov 17, 2026
… nits)

Only a float zero of either sign is replaced by 0.0; floats come from
image_summaries and a caller-given value keeps its type. Rewraps the
short line left by #110.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
@guijacquemet guijacquemet mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Signed zero: a plane holding -0.0 and +0.0 can record a different pixel_min/max on different platforms

2 participants